package com.xszx.server.service;

import com.xszx.server.dto.CourseDto;
import com.xszx.server.dto.Page;
import com.xszx.server.dto.SortDto;
import com.xszx.server.entity.*;
import com.xszx.server.mapper.CourseCategoryMapper;
import com.xszx.server.mapper.CourseMapper;
import com.xszx.server.util.CopyUtil;
import com.xszx.server.util.UuidUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class CourseService {
    @Resource
    private CourseMapper courseMapper;

    @Resource
    private CourseContentService courseContentService;

    @Resource
    private TeacherService teacherService;

    @Resource
    private SectionService sectionService;

    @Resource
    private ChapterService chapterService;

    @Resource
    private CourseCategoryService courseCategoryService;
    /**
     * 获取分页列表
     * @param page
     * @return
     */
    public Page getAllCourses(Page page){
        int pageNum = page.getPage();
        page.setPage((page.getPage()-1)*page.getSize());
        page.setList(courseMapper.findAll(page));
        page.setTotal(courseMapper.findCount(page.getCategoryId()));
        page.setPage(pageNum);
        return page;
    }

    /**
     * 新增或者删除信息
     * @param course
     * @return
     */
    @Transactional
    public String saveOrUpdate(Course course){

        if(StringUtils.isEmpty(course.getId())){
            Date now = new Date();
            course.setCreatedAt(now);
            course.setUpdatedAt(now);
            course.setId(UuidUtil.getShortUuid());
            //批量添加课程分类
            courseCategoryService.addBatch(course);
            return (courseMapper.insert(course)>0)?"添加成功":"添加失败";
        }else{
            course.setUpdatedAt(new Date());
            //批量添加课程分类
            courseCategoryService.addBatch(course);
            return (courseMapper.updateByPrimaryKey(course)>0)?"更新成功":"更新失败";
        }
    }
    /**
     * 更新排序
     * @param sortDto
     * @return
     */
    public String updateSort(SortDto sortDto){
        Course course = new Course();
        course.setId(sortDto.getId());
        course.setSort(sortDto.getNewSort());
        return (courseMapper.updateByPrimaryKeySelective(course)>0)?"更新成功":"更新失败";
    }
    /**
     * 物理删除课程
     * @param id
     * @return
     */
    public int del(String id){
        return courseMapper.deleteByPrimaryKey(id);
    }
     /**
     * 获取单个课程
     * @return
     */
    public CourseDto getCourse(String id){

        Course course = courseMapper.selectByPrimaryKey(id);

        CourseDto courseDto = CopyUtil.copy(course, CourseDto.class);

        // 查询内容
        CourseContent content = courseContentService.getCourseContent(id);
        if (content != null) {
            courseDto.setContent(content.getContent());
        }

        // 查找讲师信息
        Teacher teacherDto = teacherService.getTeacher(courseDto.getTeacherId());
        courseDto.setTeacher(teacherDto);

        // 查找章信息
        List<Chapter> chapterDtoList = chapterService.getChaptersByCourse(id);
        courseDto.setChapters(chapterDtoList);

        // 查找节信息
        List<Section> sectionDtoList = sectionService.getSectionsByCourse(id);
        courseDto.setSections(sectionDtoList);
        return courseDto;
    }
}
